/**
 * @desc
 * 依赖：
 * 1. ejs
 * 2. alert.js: 需要弹框时
 * 3. util.js :
 * Created by suweiming on 2018/3/29.
 */
/**
 *  弹框增加表单
 * @param obj 表单对象
 * @param isAlert 是否弹框
 */
function fnForm(obj, isAlert, formId) {
    var data = obj.data;
    var $formId = fnGenId();
    var html = $(window.top.document).find(formId || '#component-form');
    var form = obj.form || {};
    form.id = fnGenId();// 系统生成id
    data.formId = form.id;
    html = ejs.render(html.html(), {
        list: data,
        form: form,
        noForm: !!obj.noForm
    });

    if (isAlert) {
        layerAlert({
            title: obj.title || '表单',
            body: html,
            width: obj.width || '420',
            shown: function (index) {

                obj.shown && obj.shown({index: index});
                $('.layui-layer-content').css('overflow', 'visible');
                validateDialogForm(data, obj.submit);
            },
            closeHide: function () {

            },
            okHide: function () {

            }
        });
    } else {
        $('#' + obj.eleId).html(html);
        obj.shown && obj.shown();
       validateDialogForm(data, obj.submit, obj.custom);
    }
}
function validateDialogForm(data, submit, custom) {
    var form = {};
    data.map(function (item) {
        form[item.name] = {
            name: {
                row: '.col-xs-7',
                validators: $.extend({}, item.require ? {
                    notEmpty: {
                        message: item.label + '必填'
                    }
                } : {}, item.validator || {})
            },
        };
    });
    console.log(('body #' + data.formId));
    console.log($('body #dialog-form' + data.formId).length);
    $('body #dialog-form' + data.formId).validationEngine('attach', {
        focusFirstField: true,
        validationEventTrigger: 'input', // 触发事件，默认blur
        promptPosition: "centerRight",
        onValidationComplete: function (form, status) {
            console.log("The form status is: " + status + ", it will never submit");

            var for1 = decodeObj(_parseSearch(form.serialize()));

            if (status) {
                submit && submit({
                    form: form,
                    data: for1
                }, status)
            }
        },
        showOneMessage: true,
        onSuccess: function () {
            console.log('success !')
        },
        custom_error_messages: custom || {}
    });

    // $('body #dialog-form')
    //     .formValidation('destroy').formValidation($.extend({}, VALIDATION_SETTING, {
    //     fields: form
    // }))
    //     .off('success.form.fv').on('success.form.fv', function (e) {
    //     e.preventDefault();
    //     var form = parseSearch($(e.target).serialize());
    //     form = decodeObj(form);
    //     console.log(JSON.stringify(form));
    //     submit && submit(form);
    //
    // });
}
function layerAlert(opt) {
    if (typeof layer == 'undefined')throw new Error('layer not defined');
    var t = layer.open({
        type: opt.type || 1,
        title: opt.title,
        //  btn: ['按钮一'],
        shade: 0.3,
        anim: 5,
        area: opt.width || 'auto',
        resize: true,
        maxmin: true,
        success: function () {
        },
        yes: opt.okHide || function (item) {
        },
        cancel: opt.closeHide || function () {
        },
        //skin: 'layui-layer-rim', //加上边框
        // area: ['420px', '240px'], //宽高
        content: opt.body
    });

    opt.shown && opt.shown(t);
    return t;
}